package com.invoicing.controller;

import com.invoicing.MainController;
import com.invoicing.Service.UserService;
import com.invoicing.entity.User;
import com.invoicing.view.LoginView;
import com.invoicing.view.MenuView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ResourceBundle;

@FXMLController
public class RegisterController implements Initializable {

    @Autowired
    private UserService userService;

    @FXML
    private TextField name;

    @FXML
    private PasswordField password;

    @FXML
    private PasswordField surePassword;

    @FXML
    private Button register;

    @FXML
    private Button back;

    @FXML
    private Label success;

    @FXML
    TextField password_text;

    @FXML
    TextField surePassword_text;

    @FXML
    private CheckBox show;

    @FXML
    private void setRegister(){
        Alert alert_register = new Alert(Alert.AlertType.ERROR);
        if(name.getText().length()==0){
            alert_register.setTitle("提示");
            alert_register.setContentText("用户名不能为空！");
            alert_register.show();

        }
        else {
            if(password.getText().length()==0){
                alert_register.setTitle("提示");
                alert_register.setContentText("密码不能为空！");
                alert_register.show();
            }
            else if(surePassword.getText().length()==0){
                alert_register.setTitle("提示");
                alert_register.setContentText("请再次输入密码！");
                alert_register.show();
            }
            else {
                User user = new User();
                user.setName(name.getText());
                user.setPassword(password.getText());
                if(user.getPassword().equals(surePassword.getText())){
                    success.setText("账户"+user.getName()+"注册成功！");
                    userService.addUser(user);
                }
                else {
                    alert_register.setTitle("提示");
                    alert_register.setContentText("密码不一致！");
                    alert_register.show();
                    surePassword.clear();
                }
            }

        }
    }
    @FXML
    private void showPassword(){
        if (show.isSelected()) {
            if(password.getText().length()==0||surePassword.getText().length()==0){
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("提示");
                alert.setContentText("密码不能为空！");
                alert.show();
                show.setSelected(false);
            }
            else{
                password_text.setText(password.getText());
                password_text.setVisible(true);
                password.setVisible(false);
                surePassword_text.setText(surePassword.getText());
                surePassword_text.setVisible(true);
                surePassword.setVisible(false);
            }
            return;
        }
        password.setText(password_text.getText());
        password.setVisible(true);
        password_text.setVisible(false);
        surePassword.setText(surePassword_text.getText());
        surePassword.setVisible(true);
        surePassword_text.setVisible(false);
    }

    @FXML
    private void setBack(){
        MainController.showView(LoginView.class);
    }
    public void initialize(URL location, ResourceBundle resources) {

    }
}
